<?php

/**
* Model:  "Login_model"
*
* This model allows CodeIgniter to interact with the database - more
* specifically, the "login" table in the database.  This model checks
* login credentials entered at the login page, and verifies that the
* user is actually allowed to login.
*
* Functions:  check_login
*
* @author     Brandon A Johnson
* @copyright  LIS4368-01: "Web Development with PHP," Spring 2010
* @version    1.0
* @date       April 24, 2010
*
*/

class Login_model extends Model {

	/**
	* Login_model() - constructor
	*
	* allows this file to act as a valid CodeIgniter model
	*/
    function Login_model()
    {
        parent::Model();
		
		$this->load->database();
	}
	
	/**
	* check_login()
	*
	* takes the user-entered login information and checks it against
	* records in the database.  If the information is valid, the user
	* is redirected to the secure section.  If not, the user is told
	* that their information is incorrect and asked to try again.
	*
	* values passed: $name - the username being checked
	*                $pass - the password being checked
	*/
	function check_login($name, $pass)
	{
		// run a query to see if the username exists in the table
		$query = $this->db->get_where('login', "`username` = '$name'");

		// if it does...
		if ($query->num_rows() == 1)
		{
			// find and extract that row
			$result = $query->row();

			// if the password matches too...
			if ($result->password == $pass)
			{
				return TRUE;
			}
			// otherwise...
			else
			{
				return FALSE;
			}
		}
		// otherwise...
		else
		{
			return FALSE;
		}
	}
}

/* EOF */